//
//  BoutiqueController.m
//  Food
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BoutiqueController.h"
#import "BaseHeader.h"
#import "Boutique_classify_Cell.h"
#import "AFNetworkHandler.h"
#import "BoutiqueModel.h"
#import "Boutique_class_details_Controller.h"
@interface BoutiqueController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *dataArry;

@end

@implementation BoutiqueController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor= [UIColor colorWithRed:238 / 255.0 green:236 / 255.0 blue:232 / 255.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"分类";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatCollectionView];
    
    [self getdata];
}

- (void) creatCollectionView
{
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 60) / 3.0, (ScreenWidth - 60) / 3.0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(-5, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView .contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[Boutique_classify_Cell class] forCellWithReuseIdentifier:@"cell"];
}

- (void) getdata
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    
    self.dataArry = [NSMutableArray array];
    NSString *strurl1 = @"http://121.41.88.179:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueTypeList.do?is_traditional=0&page=1&pageRecord=12&phonetype=1";
    [AFNetworkHandler getWithAFNByURL:strurl1 completion:^(id result) {
        NSArray *tempArry1 = result[@"data"];
        self.dataArry = [BoutiqueModel arrayWithModelByArray:tempArry1];
        
        NSString *strurl2 = @"http://121.41.88.179:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueTypeList.do?is_traditional=0&page=2&pageRecord=12&phonetype=1";
        [AFNetworkHandler getWithAFNByURL:strurl2 completion:^(id result) {
            NSArray *tempArry1 = result[@"data"];
            NSArray *arry = [BoutiqueModel arrayWithModelByArray:tempArry1];
            for (BoutiqueModel *model in arry) {
                [self.dataArry addObject:model];
            }
            [self.collectionView reloadData];
            [activityIndicator stopAnimating];
        }];
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Boutique_classify_Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    BoutiqueModel *model = self.dataArry[indexPath.item];
    [cell setModel:model];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArry.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Boutique_class_details_Controller *bcdController = [[Boutique_class_details_Controller alloc] init];
    
    BoutiqueModel *model = self.dataArry[indexPath.item];
    
    bcdController.naviTitle = model.name;
    
    bcdController.classid = model.classId;
    

    [self.navigationController pushViewController:bcdController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
